//
//  WashPriceController.m
//  carowner
//
//  Created by apple on 2019/5/24.
//  Copyright © 2019 ggj. All rights reserved.
//

#import "WashPriceController.h"
#import "WashPriceCell.h"
#import "HeadTitleView.h"
#import "WashTypeList.h"
#import "WashMoneyList.h"

@interface WashPriceController ()<UITableViewDelegate,UITableViewDataSource,DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>

@property(nonatomic,strong)UITableView * dataRyc;
@property(nonatomic,strong)UIButton * sureBtn;
@property(nonatomic,strong)NSArray<WashTypeList *> * projectList;

@end

@implementation WashPriceController

- (void)viewDidLoad{
    [super viewDidLoad];
    self.title = @"洗车价格列表";
    [self initData];
}


-(void)initData{
    _sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _sureBtn.backgroundColor = color2;
    [_sureBtn setTitle:@"我知道了" forState:UIControlStateNormal];
    [_sureBtn setFont:textFont];
    [_sureBtn addTarget:self action:@selector(doSure) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_sureBtn];
    
    _dataRyc = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStylePlain];
    [_dataRyc registerClass:[WashPriceCell class] forCellReuseIdentifier:@"cellID"];
    _dataRyc.separatorStyle = UITableViewCellSeparatorStyleNone;
    _dataRyc.delegate = self;
    _dataRyc.dataSource = self;
    _dataRyc.emptyDataSetSource = self;
    _dataRyc.emptyDataSetDelegate = self;
    
    [self.view addSubview:_dataRyc];
    
    [_sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).offset(-kSafeArea_Bottom-10);
        make.left.equalTo(self.view).offset(15);
        make.right.equalTo(self.view).offset(-15);
        make.height.mas_equalTo(40);
    }];
    
    [_dataRyc mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(getRectNavAndStatusHight+15);
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(_sureBtn.mas_top).offset(-10);
    }];
    
    NSDictionary * dct = [[NSDictionary alloc] initWithObjectsAndKeys:_orgID,@"companyId", nil];
    
    [[ApiManager sharedManager] getShopWashPrice:dct andBlock:^(id data, NSError *error) {
        id resultData = [data valueForKeyPath:ResultList];
        _projectList = [WashTypeList mj_objectArrayWithKeyValuesArray:resultData];
        [_dataRyc reloadData];
    }];
}

-(void)doSure{
    [self.navigationController popViewControllerAnimated:YES];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _projectList == nil ? 0:_projectList.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    WashTypeList * typeList = [_projectList objectAtIndex:section];
    NSArray<WashMoneyList * > * list = typeList.list;
    return list.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WashPriceCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cellID"];
    WashTypeList * typeList = [_projectList objectAtIndex:indexPath.section];
    NSArray<WashMoneyList * > * list = typeList.list;
    WashMoneyList * entity = [list objectAtIndex:indexPath.row];
    cell.projectName.text = entity.project_name;
    cell.tvTime.text = [NSString stringWithFormat:@"%@%.1lf小时",@"标准工时:",entity.man_hours];
    cell.tvPrice.text = [NSString stringWithFormat:@"¥%.2lf元",entity.working_price];
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    WashTypeList * typeList = [_projectList objectAtIndex:section];
    HeadTitleView * titleView = [[HeadTitleView alloc] init];
    titleView.tvLabel.text = typeList.name;
    return titleView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}


-(NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView{
    NSString *title = @"暂无数据哦!";
    NSDictionary *attributes = @{
                                 NSFontAttributeName:[UIFont boldSystemFontOfSize:14],
                                 NSForegroundColorAttributeName:[UIColor darkGrayColor]
                                 };
    return [[NSAttributedString alloc] initWithString:title attributes:attributes];
}

- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView
{
    return YES;
}



@end
